package com.hp.jus;

import java.net.URL;
import java.util.Properties;

import com.hp.jus.core.IJUS;

/**
 * the class that loads the configurations for implemented class
 * and the configurations for the jusser itself
 * @author salemmo
 *
 */
public class Jusser {
	
	private Properties props;
	private IJUS ijus;
	private int capstate;
	private int keyLength;
	
	public Jusser(Properties props){
		try{
			this.props = props;
			
			capstate = Integer.valueOf(props.getProperty("jusser.key.caps"));
			keyLength = Integer.valueOf(props.getProperty("jusser.key.length"));
			
			ijus = (IJUS)Class.forName(props.getProperty("impl.class.name")).newInstance();
			ijus.setProperties(props);
			ijus.init();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public String add(URL url)throws Exception{
		String key = JusUtils.generateKey(url, keyLength, capstate);
		ijus.add(key, url);
		return key;
	}
	
	public String addAndgetSURL(URL url)throws Exception{
		return props.getProperty("jusser.url.prefix")+add(url);
	}
	
	public boolean remove(String key)throws Exception{
		return ijus.remove(key);
	}
	
	public URL get(String key)throws Exception{
		return ijus.get(key);
	}
	
	public boolean update(String key, URL url)throws Exception{
		return ijus.update(key, url);
	}
	
	public void close()throws Exception{
		ijus.close();
	}
}
